package com.csist.servlet;

import com.csist.bean.Classes;
import com.csist.bean.Teacher;
import com.csist.dao.ClassesDao;
import com.csist.dao.TeacherDao;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;
import java.util.List;

@WebServlet(name = "LoginServlet",urlPatterns={
        "/LoginServlet",
        "/html/LoginServlet"
})
public class LoginServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("utf-8");
        response.setCharacterEncoding("utf-8");
        response.setContentType("text/html;charset=utf-8");
        String username = request.getParameter("username");
        String password = request.getParameter("userpass");
        TeacherDao tDao = new TeacherDao();
        Teacher teacher = tDao.login(username,password);
        if (teacher!=null){
            ClassesDao cDao = new ClassesDao();
            List<Classes> classList = cDao.getClassesByTeacherId(teacher.getT_id());

            HttpSession session = request.getSession();
            session.setAttribute("classList",classList);
            session.setAttribute("teacher",teacher);
            response.sendRedirect("../jsp/welcome.jsp");
        } else {
            response.sendRedirect("../jsp/overTime.jsp");
        }
    }
}
